
function ap_showSystemDialog (sid, pos, options) {
  function btn_system_BBCode () {
    var id = $(this).attr('id');
    var bid = id + '_bb';
    var obid = $('#'+bid);
    if (obid.length == 0) {
      var x = $(this).clone();
      x.attr('id', bid);
      $(this).after(x).hide();
      
      $('#'+bid).html('<textarea id="'+bid+'_code" style="margin: 10px; width: 92%; height: 78%;"></textarea>');
      
      var sys = $(this).siblings('.ui-dialog-titlebar').children('.ui-dialog-title').text().match(/(.*?) \((-?\d+\/-?\d+)\) \[(\d+)\]$/);
      
      $('#'+bid+'_code').text('Export of System [b]'+sys[3]+' - '+sys[1]+' ('+sys[2]+')[/b]\r\n[size=13]\r\n[i]pid pop sb   owner[/i][/size][size=14]\r\n');
      
      $('table tr:gt(0)', this).each(function(i) {
        var color;
        switch ($(this).attr('bgcolor')) {
          case '#602020': color = 'red'; break;
          case '#305050': color = 'green'; break;
          case '#404040': color = 'darkgrey'; break;
        }
        var pid = $('td:eq(0)', this).text();
        pid = pid.length == 2 ? pid : '0'+pid;
        var pop = $('td:eq(1)', this).text();
        pop = pop.length == 2 ? pop : '0'+pop;
        var sb = $('td:eq(2)', this).text();
        sb = sb.length == 2 ? sb : '0'+sb;
        var user = $('td:eq(3)', this).text();
        var line = '[color='+color+']'+pid+'  '+pop+'  '+sb+'   '+user+'[/color]\r\n';
        
        $('#'+bid+'_code').append(line);
      });
      $('#'+bid+'_code').append('[/size]\r\nGenerated by [url=http://code.google.com/p/awplus/]awPlus[/url] at '+ new Date().toString());
      $('#'+bid+'_code').focus().get(0).select();
      
    }
    else {
      obid.remove();
      $(this).show();
    }
  }


  if ($('#apsys_'+ sid).dialog('open').size()) { return; }

  pos = pos || $('#'+ sid).offset();
  options = options || {};

  $('body').append('<div id="apsys_'+sid+'" class="apsys"></div>');
  
  var settings = {
    resizable: false,
    position: (typeof pos == 'object') ? [pos.left-150, pos.top-175] : 'center',
    width: 300,
    height: 350,
    buttons: {
      'Reload': function() {
        $(this).text('');
        $(this).getSystemData(sid);
      },
      'BBCode': btn_system_BBCode,
      'Close': function() { $(this).dialog('close'); }
    }
  }
  jQuery.extend(settings, options);
  $('#apsys_'+ sid).dialog(settings);

  $('#apsys_'+ sid).getSystemData(sid);
}

jQuery.fn.getSystemData = function (sid, title_on_body) {
  title_on_body = title_on_body || false;
  function initPlayer (e) {
    var pid = this.href.match(/([0-9]+)$/)[1];
    $(this).addClass('applayer');
    this.href = "javascript:void(0);";

    $(this).click(function() {
      var pos = $(this).offset();
      ap_showPlayerDialog(pid, pos);
    });
  }

  return this.each(function() {
    var object = $(this);
    object.toFront().html('Loading...');

    $.get('/0/Map/Detail.php/?nr='+sid,
      function(data) {
        var syshtml = data.replace(/\n/g, '');
        if (syshtml.length < 1600) {
          object.dialog('close').html('');
          if (title_on_body) {
            object.html('<center>System '+sid+' not available for you!</center>');
          }
          else alert('System '+sid+' not available for you!');
          return;
        }
        var systitle = $('table:eq(2) tr:eq(0) td', syshtml).html().replace('Planets at ', '') + ' ['+sid+']';
        var aphtml = $('table:eq(2)', syshtml).find('tr:eq(0)').remove().end().html();
        aphtml = '<table cellspacing="1" cellpadding="1" border="0" width="100%" style="padding: 10px;">'+aphtml+'</table>';

        if (title_on_body) {
          aphtml = '<br /><div align="center">'+systitle +'</div>'+ aphtml;
        }
        else {
          object.siblings('.ui-dialog-titlebar').children('.ui-dialog-title').html(systitle);
        }
        object.html(aphtml).find('table tr:gt(0) td a')
            .each(initPlayer)
            .end();
      });
  });
}
  
  

function ap_showPlayerDialog (pid, pos) {
  function getPlayerData (pid) {
    $('#appla_'+ pid).toFront().html('Loading...');

    $.get('/0/Player/Profile.php/?id='+pid,
      function(data) {
        var plahtml = data.replace(/\n/g, '');
        var aphtml = '<table width="100%" style="padding: 10px;">';
        aphtml += $('table:eq(2)', plahtml).html();
        aphtml += '</table>';

        var aptitle = $('table tr td center', aphtml).html().replace('Premium Member', '');

        $('#appla_'+ pid)
          .siblings('.ui-dialog-titlebar').children('.ui-dialog-title').html(aptitle).end()
        $('#appla_'+ pid)
          .html(aphtml)
          .find('table:eq(1) tr:eq(0) td center')
            .append('&nbsp;['+pid+']')
          .end()

        if ($('#appla_'+pid+' table:eq(0) tr:eq(0) > td').size() == 2) {
          $('#appla_'+pid).parents('.ui-dialog').width(460).height(450).end().width(460);
        }
      });   
  }

  if ($('#appla_'+ pid).dialog('open').size()) { return; }

  $('body').append('<div id="appla_'+pid+'" class="appla"></div>');
  
  $('#appla_'+ pid).dialog({
      resizable: false,
      position: (typeof pos == 'object') ? [pos.left-135, pos.top-205] : 'center',
      width: 270,
      height: 410,
      dialogClass: 'appladet',
      buttons: {
        'Reload': function() {
          $(this).empty();
          getPlayerData(pid);
        },
        'Close': function() { $(this).dialog('close'); }
      }
    });

  getPlayerData(pid);   
}


var jq_zindex = 100;
jQuery.fn.toFront = function() {
  return this.each(function() {
    $(this).css('z-index', ++jq_zindex);
  });
}

function ap_player_attachClickByClass(selector) {
  selector = selector || '.ap_player';
  $(selector).click(function(i) {
    var pid = this.id.match(/_(\d+)$/)[1];
    ap_showPlayerDialog(pid);
  });
}

$().ready(function() {

  // Adding a css for dialog style.
  $('head').append('<link rel="stylesheet" href="http://mutantepower.com.br/awplus-ui/jquery-ui-themeroller2.css" type="text/css" media="screen">');

  // apply all player links
  $('a[href*=Player/Profile.php]').each(function(i) {
    var xpid = $(this).attr('href').match(/id=([0-9]+)$/)[1];
    this.id = 'ap_player_'+i+'_'+xpid;
    this.href = 'javascript: void(0);';
    $(this)
      .addClass('ap_player')
      .click(function() {
        ap_showPlayerDialog(xpid);
      });
  });
  
  $('tr[onclick]').each(function () {
    var x = $(this).attr('onclick');
    if (x.toString().match(/Player\/Profile/)) {
      $(this).attr('onclick', '');
    }
  });

});
